<template>
  <div>
    <div class="filter-container">
      <el-form size="mini">
        <el-form-item label="用户">
          <el-input v-model="filters.name" placeholder="筛选用户"></el-input>
        </el-form-item>
        <el-form-item label="操作">
          <el-input v-model="filters.action" placeholder="筛选操作"></el-input>
        </el-form-item>
      </el-form>
    </div>
    <el-table
      :data="paginatedData"
      style="width: 100%"
      :default-sort="{ prop: 'date', order: 'descending' }"
    >
      <el-table-column prop="name" label="用户"></el-table-column>
      <el-table-column prop="date" label="时间" sortable="custom"></el-table-column>
      <el-table-column prop="action" label="操作"></el-table-column>
      <el-table-column prop="status" label="结果"></el-table-column>
    </el-table>
    <el-pagination
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-size="pageSize"
      :total="filteredData.length"
      layout="total, prev, pager, next"
      style="margin-top: 20px; text-align: right;"
    ></el-pagination>
  </div>
</template>


<script>
export default {
  data() {
    return {
      filters: {
        name: "",
        action: "",
        status: ""
      },
      currentPage: 1,
      pageSize: 5,
      tableData: []
    };
  },
  computed: {
    filteredData() {
      return this.tableData
        .filter(item => {
          return (
            (this.filters.name === "" ||
              item.name.includes(this.filters.name)) &&
            (this.filters.action === "" ||
              item.action.includes(this.filters.action))
          );
        })
        .sort((a, b) => {
          return new Date(b.date) - new Date(a.date);
        });
    },
    paginatedData() {
      return this.filteredData.slice(
        (this.currentPage - 1) * this.pageSize,
        this.currentPage * this.pageSize
      );
    }
  },
  methods: {
    handleCurrentChange(val) {
      this.currentPage = val;
    }
  },
  created() {
    const localtoken = localStorage.getItem("token");
    this.$axios
      .get("http://47.108.248.93:999/SysLog", {
        headers: {
          token: localtoken
        }
      })
      .then(res => {
        this.tableData = res.data.data;
      });
  }
};
</script>
<style scoped>
.filter-container {
  width: 500px;
}
</style>

